package cn.itcast.bos.web.action.base;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.CourierService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Controller
@Namespace("/")
@Scope("prototype")
public class CourierAction extends BaseAction<Courier>{
	
	@Autowired
	private CourierService courierService;
	
	//添加快递员
	@Action(value="courier_save",results={@Result(name="success",location="./pages/base/courier.html",type="redirect")})
	public String save() {
		courierService.save(model);
		return SUCCESS;
	}
	

	//由于多条件的查询json和datagrid表格进行了绑定，所以又提交到同一个方法
	//分页查询快递员信息
	@Action(value="courier_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery() {
		
		//对于参数进行判断用到接口JpaSpecificationExecute
		
		Specification<Courier> specification=new Specification<Courier>() {
			@Override
			public Predicate toPredicate(Root<Courier> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				//一共三个参数root是Courier的对象，query是简单地条件查询，CriteriaBuilder是复杂的条件查询
				 
				//为了防止cb.and(Predicate)的覆盖效果，建立数组
				List<Predicate> predicates=new ArrayList<Predicate>();
				//先判断简单的数据是否有值
				//工号 String courierNum
				if (StringUtils.isNotBlank(model.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
				   //加入到数组中
					predicates.add(p1);
				}
				//所属单位String company
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%");
				    
					predicates.add(p2);
				}
				
				//类型 String type
				if (StringUtils.isNotBlank(model.getType())) {
					Predicate p3 = cb.equal(root.get("type").as(String.class), model.getType());
				    predicates.add(p3);
				}
				
				
				//多标的关联查询联系
				Join<Courier, Standard> standJoin=root.join("standard",JoinType.INNER);
				if (model.getStandard()!=null && StringUtils.isNoneBlank(model.getStandard().getName())) {
					Predicate p4 = cb.equal(standJoin.get("name").as(String.class), model.getStandard().getName());
				    predicates.add(p4);
				}
				
				Predicate predicate = cb.and(predicates.toArray(new Predicate[0]));
				return predicate;
			}
		};
		Pageable pageable=new PageRequest(page-1, rows);
		Page<Courier> page=courierService.pageQuery(specification,pageable);
		pushPageDataToValueStack(page);
		return SUCCESS;
	}
	

	//逻辑性的快递员的删除
	@Action(value="courier_delflag",results={@Result(name="success",location="./pages/base/courier.html",type="redirect")})
	public String delflag() {
			courierService.delflag(tag,ids);
		return SUCCESS;
	}
	
	//查询没有关联定区的快递员
	@Action(value="courier_findNoAssociation",results={@Result(name="success",type="json")})
	public String findNoAssociation() {
		//定区和快递员是多对多的关系
		List<Courier> couriers=courierService.findNoAssociation();
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}
	private String fixedAreaId;
	
	public void setFixedAreaId(String fixedAreaId) {
		this.fixedAreaId = fixedAreaId;
	}

	//定区关联快递员的查询
	@Action(value="fixedArea_associationCourier",results={@Result(name="success",type="json")})
	public String  fixedAreaAssociationCourier() {
		List<Courier> couriers=courierService.fixedAreaAssociationCourier(fixedAreaId);
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}
 
}
